package com.manshutingyu.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.manshutingyu.common.core.dto.Option;
import com.manshutingyu.common.core.dto.R;
import com.manshutingyu.common.core.dto.RouterDTO;
import com.manshutingyu.system.domain.SysMenu;
import com.manshutingyu.system.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author：yinyuxia
 * @Date：2025/3/29 20:46
 */
@Slf4j
@RestController
@Tag(name = "菜单管理")
@RequestMapping("/sys/menu")
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;

    @Operation(summary = "获取菜单路由信息")
    @GetMapping("/getRouters")
    public R<List<RouterDTO>> getRouters() {
        List<SysMenu> menus = sysMenuService.getRouters(null, true);
        return R.success(sysMenuService.buildMenus(menus));
    }

    @Operation(summary = "菜单下拉列表")
    @GetMapping("/options")
    public R<?> listMenuOptions(@Parameter(description = "是否只查询父级菜单")
                                @RequestParam(value = "onlyParent", required = false, defaultValue = "false") Boolean onlyParent) {
        List<Option> menus = sysMenuService.listMenuOptions(onlyParent);
        return R.success(menus);
    }

    @Operation(summary = "获取菜单列表")
    //  @PreAuthorize("@ss.hasPermission('sys_menu_query')")
    @GetMapping("list")
    public R<List<SysMenu>> list(SysMenu menu) {
        List<SysMenu> menus = sysMenuService.selectMenuList(menu, null, true);
        return R.success(menus);
    }


    @Operation(summary = "分页查询")
    //  @PreAuthorize("@ss.hasPermission('sys_menu_page')")
    @GetMapping(value = "/page")
    public R<IPage<SysMenu>> page(@ParameterObject Page<SysMenu> page, @ParameterObject SysMenu sysMenu) {
        return R.success(sysMenuService.page(page, Wrappers.lambdaQuery(sysMenu)));
    }


    @Operation(summary = "详情")
    // @PreAuthorize("@ss.hasPermission('sys_menu_get')")
    @GetMapping(value = "/{id}")
    public R<SysMenu> getById(@PathVariable("id") Long id) {
        return R.success(sysMenuService.getById(id));
    }



    @PostMapping
    public R<Integer> save(@RequestBody SysMenu sysMenu) {
        return sysMenuService.saveMenu(sysMenu) ? R.success() : R.error();
    }



    @PutMapping
    public R<Integer> update(@RequestBody SysMenu sysMenu) {
        return sysMenuService.saveMenu(sysMenu) ? R.success() : R.error();
    }



    @DeleteMapping(value = "/{ids}")
    public R<Integer> deleteByIds(@Parameter(description = "多个以英文逗号(,)拼接") @PathVariable("ids") Long[] ids) {
        return sysMenuService.removeBatchByIds(Arrays.asList(ids)) ? R.success() : R.error();
    }
}
